package com.cwf.business.util;

import android.app.Activity;
import android.support.annotation.Keep;
import android.view.Window;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by sam_chan on 2017/5/8.
 */
@Keep
public class DFStatusBar {

    public static boolean setLightStatusBar(Activity activity) {
        return setStatusBarMode(activity, false) != -1;
    }

    public static boolean setDarkStatusBar(Activity activity) {
        return setStatusBarMode(activity, true) != -1;
    }

    private static int setStatusBarMode(Activity activity, boolean isDark) {
        int result = -1;
        if (setMIUIStatusBarMode(activity.getWindow(), isDark)) {
            result = 1;
        }
        else if (setFlymeStatusBarMode(activity.getWindow(), isDark)) {
            result = 2;
        }
        return result;
    }

    /**
     * 设置状态栏图标为深色和魅族特定的文字风格
     * 可以用来判断是否为 fly-me 用户
     */
    private static boolean setFlymeStatusBarMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                }
                else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception ignored) {

            }
        }
        return result;
    }

    private static boolean setMIUIStatusBarMode(Window window, boolean dark) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();
            try {
                int darkModeFlag = 0;
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
                if (dark) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
                }
                else {
                    extraFlagField.invoke(window, 0, darkModeFlag);
                }
                result = true;
            } catch (Exception ignored) {

            }
        }
        return result;
    }

    public static boolean canModifyStatusBar(Window window) {
        if(isXiaoMi(window)){
            return true;
        }

        return isMeiZhi(window);

    }


    private static boolean isXiaoMi(Window window) {
        try {
            Class clazz = window.getClass();
            Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            field.getInt(layoutParams);
            return true;
        } catch (Exception ignored) {

        }
        return false;
    }

    private static boolean isMeiZhi(Window window) {
        try {
            Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
            Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
            darkFlag.setAccessible(true);
            meizuFlags.setAccessible(true);
            return true;
        } catch (Exception ignored) {

        }
        return false;
    }


}
